layui.use(['table', 'jquery', 'layer'], function (data) {
    var table = layui.table,
        layer = layui.layer,
        $ = layui.jquery;


    var tableIns = table.render({
        elem: "#purchaseRequestList",
        height: 'full-125',
        url: ctx + '/supplier/selectPurchaseRequest',
        page: true,
        cellMinWidth: 80, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[// 表头
            {type: 'checkbox'},
            {
                title: "采购单号", field: "purchaseNo", templet: function (data) {
                    if (data.purchaseNo == null) {
                        return '';
                    } else {
                        return data.purchaseNo;
                    }
                }
            },
            {title: "项目编号", field: "projectNo"},
            {title: "物料编号", field: "materialNo"},
            {title: "物料名称", field: "materialName"},
            {title: "物料数量", field: "purchaseCount"},
            {title: "预估金额", field: "purchasePrice"},
            {
                title: "类型", field: "orderType", templet: function (data) {
                    if (data.orderType == 0) {
                        return "原材料购买"
                    }
                    if (data.orderType == 1) {
                        return "生产补料"
                    }
                }
            },
            {
                title: "状态", field: "status", templet: function (data) {
                    if (data.orderStatus == 0) {
                        return "审批中"
                    }
                    if (data.orderStatus == 1) {
                        return "在途"
                    }
                    if (data.orderStatus == 2) {
                        return "质检中"
                    }
                    if (data.orderStatus == 3) {
                        return "已入库"
                    }
                }
            }
        ]],

    });
    // 监听头
    table.on('toolbar(purchaseRequestFilter)', function (data) {
        if (data.event == "assign") {
            var checkStatus = table.checkStatus(data.config.id);
            var data = checkStatus.data;
            openAssignDialog(data);
        }


        if (data.event == "arrival") {
            var checkStatus = table.checkStatus(data.config.id);
            var data = checkStatus.data;
            openArrivalDialog(data);
        }
    })

    function openAssignDialog(data) {

        if (data.length == 0) {
            layer.msg("请选择订单审批", {icon: 6})
            return;
        }
        for (var i = 0; i < data.length; i++) {
            if (data[i].orderStatus == 3) {
                layer.msg("选择错误，包含已入库订单", {icon: 6})
                return;
            }
            if (data[i].status == 2) {
                layer.msg("选择错误，包含正在质检的订单", {icon: 6})
                return;
            }
            if (data[i].orderStatus == 1) {
                layer.msg("选择错误，包含已经申请完成的订单", {icon: 6})
                return;
            }
        }

        layer.confirm("确认审批同意吗", {icon: 3, title: "审批采购申请单"}, function (index) {
                layer.close(index);
                $.ajax({
                    type: "post",
                    url: ctx + "/supplier/assignPurchaseRequestOrder",
                    data: JSON.stringify(data),
                    contentType: "application/json",

                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg(data.msg, {icon: 6})
                            tableIns.reload();
                        } else {
                            layer.msg(data.msg)
                        }
                    },
                    error: function (data) {
                        layer.msg("error")
                    }
                })
            }
        )
    }

    function openArrivalDialog(data) {
       console.log(data)
        if (data.length == 0) {
            layer.msg("请选择到货订单", {icon: 6});
            return;
        }
        for (var i = 0; i < data.length; i++) {
            if (data[i].orderStatus == 3) {
                layer.msg("选择错误，包含已完成订单", {icon: 6})
                return;
            }
            if (data[i].orderStatus == 2) {
                layer.msg("选择错误，包含正在质检中的订单", {icon: 6})
                return;
            }
            if (data[i].orderStatus == 0) {
                layer.msg("选择错误，包含正在审批的订单", {icon: 6})
                return;
            }
        }


        layer.confirm("确认到货转质检吗", {icon: 5, title: "采购到货"}, function (index) {
            layer.close(index);
            $.ajax({
                type: "post",
                data: JSON.stringify(data),
                url: ctx + "/quality/arrivalPurchaseRequestOrder",
                contentType: "application/json",
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg("success");
                        tableIns.reload();
                    } else {
                        layer.msg("error")
                    }
                },
                error: function (data) {
                    layer.msg("error")
                }
            })
        })

    }
})